package com.aries.remoteplusserver.core;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class KeyboardHandler {

	private Robot robot;
	
	public KeyboardHandler() throws AWTException
	{
		this.robot = new Robot();
		this.robot.setAutoDelay(5);
	}
	
	public void keyBoardHandler(Object[] args)
	{
		int keyCode = Integer.parseInt(args[0].toString());
		
		switch ( keyCode )
		{
			case KeyEvent.VK_CONTROL:
			{
				if ( Boolean.parseBoolean(args[1].toString()) )
				{
					this.robot.keyPress(KeyEvent.VK_CONTROL);
				}
				else 
				{
					this.robot.keyRelease(KeyEvent.VK_CONTROL);
				}
				
				break;
			}
			
			case KeyEvent.VK_SHIFT:
			{
				if ( Boolean.parseBoolean(args[1].toString()) )
				{
					this.robot.keyPress(KeyEvent.VK_SHIFT);
				}
				else 
				{
					this.robot.keyRelease(KeyEvent.VK_SHIFT);
				}
				
				break;
			}
			
			case KeyEvent.VK_ALT:
			{
				if ( Boolean.parseBoolean(args[1].toString()) )
				{
					this.robot.keyPress(KeyEvent.VK_ALT);
				}
				else 
				{
					this.robot.keyRelease(KeyEvent.VK_ALT);
				}
				
				break;
			}
			
			case KeyEvent.VK_WINDOWS:
			{
				if ( Boolean.parseBoolean(args[1].toString()) )
				{
					this.robot.keyPress(KeyEvent.VK_WINDOWS);
				}
				else 
				{
					this.robot.keyRelease(KeyEvent.VK_WINDOWS);
				}
				
				break;
			}
			
			case KeyEvent.VK_CAPS_LOCK:
			{
				if ( Boolean.parseBoolean(args[1].toString()) )
				{
					this.robot.keyPress(KeyEvent.VK_CAPS_LOCK);
				}
				else 
				{
					this.robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
				}
				
				break;
			}
			
			default:
			{
				this.robot.keyPress(keyCode);
				this.robot.keyRelease(keyCode);
				break;
			}
		}
	}
}
